﻿using DjvuNet;
using System.Diagnostics;
static void ConvertFile(string djvuFile, string desFolder)
{
    if (!File.Exists(djvuFile))
    {
        Console.WriteLine($"文件不存在{djvuFile}");
        Console.ReadKey();
        return;
    }
    DjvuDocument doc = new DjvuDocument(djvuFile);
    int pageCnt = 0;
    //.Take(2)
    foreach (var item in doc.Pages)
    {
        string pageImageFile = Path.Combine(desFolder, $"{++pageCnt}.jpg");
        if (!File.Exists(pageImageFile))
        {
            item.Image.Image.Save(pageImageFile, System.Drawing.Imaging.ImageFormat.Jpeg);
            Console.Write(".");
        }
        else
        {
            Console.Write(" ");
        }
    }
    doc.Dispose();
}
//string fn = @"F:\djvu文件\13070105.djvu";
//string fn = @"D:\dl\13070107参观与实习荣绪京华印书局北平.djvu";
string djvuFolder = "";
string imagesFolder = "";
#if DEBUG
{
    djvuFolder = @"D:\dl\djvus";
    imagesFolder = @"D:\dl\imgs";

}
#else
{
    Console.WriteLine("Input djvu files folder:");
    string djvuFolderRaw = Console.ReadLine().Trim();
    djvuFolder = Path.GetFullPath(djvuFolderRaw);
    Console.WriteLine("Output image files folder:");
    string imagesFolderRaw = Console.ReadLine().Trim();
    imagesFolder = Path.GetFullPath(imagesFolderRaw);
    Console.WriteLine(djvuFolder);
    Console.WriteLine(imagesFolder);
}
#endif
Console.WriteLine();
Console.WriteLine($"输入文件夹：{djvuFolder}");
Console.WriteLine($"输出文件夹:{imagesFolder}");
Console.WriteLine();


foreach (string djvuFile in Directory.GetFiles(djvuFolder, "*.djvu", SearchOption.AllDirectories))
{
    Console.Write(djvuFile);
    string desFolder = Path.GetFullPath(Path.GetDirectoryName(djvuFile)).Replace(djvuFolder, imagesFolder);
    string desImagesFolder = Path.Combine(desFolder, Path.GetFileNameWithoutExtension(djvuFile));
    Directory.CreateDirectory(desImagesFolder);
    ConvertFile(djvuFile, desImagesFolder);
    Console.WriteLine();
}
Console.WriteLine("程序结束。");
Console.ReadKey();
